﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BUS;
using DTO;
namespace EC21_EDiamond
{
    public partial class TaiKhoanKhachHang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] == null)
            {
                Response.Redirect("~/dangnhap.aspx");
            }
            if (!IsPostBack)
            {
                LoadThongTinKhachHang();
                LoadDanhSachTinh();
            }
        }
        void LoadDanhSachTinh()
        {
            TinhBUS tinhbus = new TinhBUS();
            drpTinh.DataSource = tinhbus.LoadDanhSachTinh();
            drpTinh.DataTextField = "TenTinh";
            drpTinh.DataValueField = "MaTinh";
            drpTinh.DataBind();
        }
        static int makh = 0;
        void LoadThongTinKhachHang()
        {
            KhachHangBUS khbus=new KhachHangBUS();
            KhachHangDTO kh = new KhachHangDTO();
            kh = khbus.LoadThongTinKhachHang(Session["user"].ToString());
            makh = kh.MaKH;
            txtTaiKhoan.Text = Session["user"].ToString();
            txtTenUser.Text = kh.TenKH;
            txtDiaChi.Text = kh.DiaChi;
            drpTinh.SelectedValue = kh.MaTinh.ToString();
            txtDienThoai.Text = kh.SoDT;
            txtEmail.Text = kh.Email;
            lblDiemTichLuy.Text = kh.DiemTichLuy.ToString(); ;
        }

        protected void btnDongY_Click(object sender, EventArgs e)
        {
            KhachHangBUS khbus = new KhachHangBUS();
            KhachHangDTO kh  = new KhachHangDTO();
            kh.MaKH = makh;
            kh.TenKH = txtTenUser.Text;
            kh.DiaChi = txtDiaChi.Text;
            kh.MaTinh = int.Parse(drpTinh.SelectedValue);
            kh.SoDT = txtDienThoai.Text;
            kh.Email=txtEmail.Text;
            kh.Tinhtrang = 1;
            if (khbus.CapNhatKhachHang(kh) != 0)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Cập Nhật Thông Tin Thành Công!');", true);
                LoadThongTinKhachHang();
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Cập Nhật Thông Tin Thất Bại!');", true);
            }
        }
    }
}